package com.hw.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hw.shortlink.project.dao.entity.LinkOsStats;
import com.hw.shortlink.project.dto.req.ShortLinkGroupStatsReqDto;
import com.hw.shortlink.project.dto.req.ShortLinkStatsReqDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface LinkOsStatsMapper extends BaseMapper<LinkOsStats> {

    /**
     * 记录短链接访问操作系统数据
     * @param linkOsStats
     */
    @Insert("INSERT INTO  tb_link_os_stats(full_short_url, date, cnt, os, create_time, update_time, del_flag) " +
            "VALUES( #{reqDto.fullShortUrl}, #{reqDto.date}, #{reqDto.cnt}, #{reqDto.os}, " +
            "NOW(), NOW(), 0) " +
            "ON DUPLICATE KEY UPDATE " +
            "cnt = cnt +  #{reqDto.cnt}")
    void shortLinkOsStats(@Param("reqDto") LinkOsStats linkOsStats);


    /**
     * 查询某个短链接的操作系统访问数据
     * @param shortLinkStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    os, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    tb_link_os_stats " +
            "WHERE " +
            "    full_short_url = #{reqDto.fullShortUrl} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid,os")
    List<LinkOsStats> listOsStatsByShortLink(@Param("reqDto") ShortLinkStatsReqDto shortLinkStatsReqDto);


    //todo fix
    /**
     * 查询某个分组的操作系统访问数据
     * @param shortLinkGroupStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    os, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    tb_link_os_stats " +
            "WHERE " +
            "     gid = #{reqDto.gid} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    os")
    List<LinkOsStats> listOsStatsByGroup(@Param("reqDto") ShortLinkGroupStatsReqDto shortLinkGroupStatsReqDto);
}
